//
//  PopoverAnimator.swift
//  Super_WeiBo
//
//  Created by SuperMac on 16/3/17.
//  Copyright © 2016年 lc. All rights reserved.
//

import UIKit

// 通知名字
let PopoviewAnimatorWillShow = "PopoviewAnimatorWillShow"
let PopoviewAnimatorWillDismiss = "PopoviewAnimatorWillDismiss"

class PopoverAnimator: NSObject ,UIViewControllerTransitioningDelegate , UIViewControllerAnimatedTransitioning
{
    /// 接受弹出视图frame
    var presentFrame = CGRectZero
    
    /// 记录菜单是否展开
    var isPresnent = false
    
    //MARK: - UIViewControllerTransitioningDelegate
    // 告诉系统谁来负责转场动画
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        let pVC = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pVC.presentFrame = presentFrame
        return pVC
    }
    
    // 谁来负责modal动画展示
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresnent = true
        // 通知title箭头改变
        NSNotificationCenter.defaultCenter().postNotificationName(PopoviewAnimatorWillShow, object: nil)
        return self
    }
    
    // 谁来负责modal动画消失
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresnent = false
        // 通知title箭头改变
         NSNotificationCenter.defaultCenter().postNotificationName(PopoviewAnimatorWillDismiss, object: nil)
        return self
    }
    
    //MARK: - UIViewControllerAnimatedTransitioning
    // 动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 0.5
    }
    // 动画效果
    func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        if isPresnent
        {// 展开
            // 拿到展现视图
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            // 将toview添加到容器视图上
            transitionContext.containerView()?.addSubview(toView)
            // 设置锚点 默认是在视图中心
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                
                toView.transform = CGAffineTransformIdentity
                
                }) { (_) -> Void in
                    transitionContext.completeTransition(true)
            }
        }
        else
        {// 关闭
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            
            UIView.animateWithDuration(transitionDuration(transitionContext)/2, animations: { () -> Void in
                
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.000001)
                
                }, completion: { (_) -> Void in
                    
                    transitionContext.completeTransition(true)
                    
            })
        }
    }
}